package mytest.ssw.www.myandroidproject.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;


import mytest.ssw.www.myandroidproject.R;
import mytest.ssw.www.myandroidproject.adapter.Mybaseadapter;
import mytest.ssw.www.myandroidproject.tools.MD5Utils;

public class SelfActivity extends AppCompatActivity {
    GridView gridview;
    SharedPreferences sp;
    private static String[] names = {"手机防盗", "通讯卫士", "软件管理", "进程管理", "流量统计", "手机杀毒", "缓存清理", "高级工具", "设置中心"};
    private static int[] picturess = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e, R.mipmap.f, R.mipmap.g, R.mipmap.h, R.mipmap.i};
    final private int entersetupactivity=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_self);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        gridview = (GridView) findViewById(R.id.gridview);
        Mybaseadapter mybaseadapter = new Mybaseadapter(this, names, picturess);
        gridview.setAdapter(mybaseadapter);
        gridview.setOnItemClickListener(new onclink());
         sp = getSharedPreferences("config", MODE_PRIVATE);

    }

    class onclink implements AdapterView.OnItemClickListener {


        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0: {
                    if (issetpassword())
                    {

                        inputpassword();
                    }
                     else {

                        Settingnewpassworddialog();
                    }
                    Toast.makeText(SelfActivity.this, "0", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 1: {
                    Toast.makeText(SelfActivity.this, "1", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 2: {
                    Toast.makeText(SelfActivity.this, "2", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 3: {
                    Toast.makeText(SelfActivity.this, "3", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 4: {
                    Toast.makeText(SelfActivity.this, "4", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 5: {
                    Toast.makeText(SelfActivity.this, "5", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 6: {
                    Toast.makeText(SelfActivity.this, "6", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 7: {
                    Toast.makeText(SelfActivity.this, "7", Toast.LENGTH_SHORT).show();
                    break;
                }
                case 8: {
                    Intent intent = new Intent(SelfActivity.this, SettingActivity.class);
                    startActivity(intent);
                    break;
                }
            }
        }
    }
          public boolean issetpassword()
       {
           String password = sp.getString("password",null);
               return !TextUtils.isEmpty(password);
       }

     String password,passwordagain;

     EditText edipassword , edipasswordagain;
    AlertDialog dia;
    public void  Settingnewpassworddialog()
    {
        Button yesbt,nobt;


         AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("没有设置密码");
        dialog.setMessage("请先设置密码");
        View view = View.inflate(this, R.layout.settiingpassword,null);
        yesbt = (Button)view.findViewById(R.id.yesBt);
        nobt = (Button)view.findViewById(R.id.noBt);
        edipassword =(EditText)view.findViewById(R.id.edipassword);
        edipasswordagain=(EditText)view.findViewById(R.id.edipasswodagain);
        dialog.setView(view);
        dialog.setCancelable(false);
        yesbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (checknewpaswrd(edipassword.getText().toString(), edipasswordagain.getText().toString())) {
                    savepasswrd(edipassword.getText().toString());
                    dia.dismiss();
                } else {
                    Toast.makeText(SelfActivity.this,"请重新输入",Toast.LENGTH_SHORT).show();
                }

            }


        });
        nobt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //

            }
        });

        dia=  dialog.show();
    }

    private void savepasswrd(String password) {

        SharedPreferences.Editor editor = sp.edit();
        if(password.length()>1) {
            editor.putString("password",   MD5Utils.md5password(password));
            editor.commit();
        }

    }


    private void inputpassword()
    {
        Button yesbt,nobt;
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage("请输入密码");
        View view = View.inflate(this, R.layout.inputpassword,null);
        yesbt = (Button)view.findViewById(R.id.yesBt);
        nobt = (Button)view.findViewById(R.id.noBt);
        edipassword =(EditText)view.findViewById(R.id.edipassword);
        dialog.setView(view);
        dia= dialog.show();
        dia.setCancelable(false);
        yesbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              password = edipassword.getText().toString().trim();

                String savepassword = sp.getString("password","");

                if (savepassword.equals(MD5Utils.md5password(password))) {
                    //进入引导
                    EnterLostFindactivity();
                    dia.dismiss();
                }
                else
                {
                    Toast.makeText(SelfActivity.this,"密码错了",Toast.LENGTH_SHORT).show();
                }
              // dialog1.dismiss();
            }
        });
        nobt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dia.dismiss();
            }
        });

    }

     public boolean checknewpaswrd(String password,String passwordagain)
     {

       if ( password.length()>0&&password.equals(passwordagain)) {
           return true;
       }
         else
       {
           return false;
       }
     }
Handler mhandler = new Handler()
    {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what)
        {
            case entersetupactivity :
            {
                EnterLostFindactivity();
            }
        }

    }
};

    public void  EnterLostFindactivity()
    {
        Intent intent= new Intent(this,LostFindActivity.class);
        startActivity(intent);
    }

}
